package com.two.lbms.hjx.controller;

import com.two.lbms.base.BaseController;
import com.two.lbms.base.ResultInfo;
import com.two.lbms.hjx.model.BookQuery;
import com.two.lbms.hjx.model.RenewQuery;
import com.two.lbms.hjx.service.BookInfoService;
import com.two.lbms.syt.dto.LendDTO;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * @author hjx
 * @version V1.0
 * @Date 2023/8/2 16:48
 */
@RestController
@RequestMapping("book")
@Slf4j
public class BookController extends BaseController {

    @Resource
    private BookInfoService bookInfoService;

    @GetMapping("list")
    public Map<String, Object> getBookList(BookQuery bookQuery){
        return bookInfoService.queryByParamsForTable(bookQuery);
    }
    @PostMapping("add")
    public ResultInfo addBook(BookQuery bookQuery){
        log.info("bookQuery:{}",bookQuery);
        bookInfoService.addBook(bookQuery);
        return success();
    }
    @PutMapping("update")
    public ResultInfo updateBook(BookQuery bookQuery){
        log.info("bookQuery:{}",bookQuery);
        bookInfoService.updateBook(bookQuery);
        return success();
    }

    @DeleteMapping("delete")
    public ResultInfo deleteBook(Integer id){
        bookInfoService.deleteBook(id);
        return success();
    }
    @DeleteMapping("deleteBatch")
    public ResultInfo deleteBatchBook(Integer[] ids){
        log.info("ids:{}", Arrays.toString(ids));
        bookInfoService.deleteBatchBook(ids);
        return success();
    }
    @PostMapping("checkRenew")
    public ResultInfo checkRenew(RenewQuery renewQuery, HttpServletRequest request){
        log.info("renewQuery:{}",renewQuery);
        LendDTO lendDTO = bookInfoService.checkRenew(renewQuery);
        request.setAttribute("lent", lendDTO);
        return success(lendDTO);
    }
    @PostMapping("renewBook")
    public ResultInfo renewBook(RenewQuery renewQuery,HttpServletRequest request){
        log.info("renewQuery:{}",renewQuery);
        bookInfoService.renewBook(renewQuery,request);
        return success();
    }

}
